var ns, toast, actionsheet, UIAlbumBrowser;
var app = new Vue({
    el: '#app',
    data: {
        uid: '',
        id: 0,
        show: false,
        orderlist: [],
        history: [],
        form: {
            keywords: '',
            list: [],
            pageindex: 0,
            pagesize: 10,
        },
    },
    created() {

    },
    methods: {
        // 搜索确认搜索
        onSearch: function () {
            var that = this
            that.get_data()
        },
        // 快捷搜索
        handQuickSearch: function (e) {
            var that = this
            that.form.keywords = e
            that.get_data()
        },
        // 清空历史
        handClearHistory: function () {
            var that = this;
            api.setPrefs({
                key: that.uid,
                value: ''
            })
            that.history=[];
            that.form.keywords='';
            that.get_data()
        },
        // 获取商品搜索历史记录
        getSearchKeywordHistory4Prd: function () {
            var that = this
            // 获取用户ID
            api.getPrefs({
                key: 'uid'
            }, function (retuid, err) {
                if (retuid && retuid.value) {
                    const uid = retuid.value;
                    that.uid = uid;
                    if (uid != '') {
                        // 用户搜索历史
                        api.getPrefs({
                            key: uid
                        }, function (ret) {
                            if (ret && ret.value) {
                                that.history = ret.value.split(',')
                            }
                        })
                    }
                }
            });
        },
        // 删除某一个历史标签
        // handClearHistory(e){
        //     const history = this.form.history.filter(res=> res != e)
        //     api.setPrefs({
        //         key: this.uid,
        //         value:history.join(',')
        //     })
        //     this.form.history = history
        // },
        get_data: function () {
            var that = this;
            that.pullOrderList();
        },
        pullOrderList: function () {
            var that = this
            const keywords = that.form.keywords;
            const params = {
                action: 'getorderlist',
                status: 0,//0 全部
                keywords: keywords,
                startRowIndex: that.form.pageindex,
                pageSize: that.form.pagesize,
            }
            if (keywords != '') {
                const history = that.history.concat(keywords)
                const newHistory = that.removeDuplicate(history)
                api.setPrefs({
                    key: that.uid,
                    value: newHistory.join(',')
                })
                that.history = newHistory
            }
            ns.post(params, function (succeed, data, err) {
                if (succeed) {
                    that.orderlist = data.data.List;
                } else if(err) {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            })
        },
        tel_phone: function (telPhone) {
            if (!telPhone || telPhone == undefined) {
                api.toast({
                    msg: '电话号码为空',
                    duration: 2000,
                    location: 'bottom'
                });
                return;
            }
            api.call({
                type: 'tel_prompt',
                number: telPhone
            });
        },
        // 数组去从
        removeDuplicate(arr) {
            var that = this
            if (!Array.isArray(arr)) {
                return
            }
            var arrry = [];
            var obj = {};
            for (var i = 0; i < arr.length; i++) {
                if (!obj[arr[i]]) {
                    arrry.push(arr[i])
                    obj[arr[i]] = 1
                } else {
                    obj[arr[i]]++
                }
            }
            return arrry;
        },
        //订单进入详情
        open_order_detail: function (id) {
            var that = this;
            ns.openWin('dl_order_detail', '订单详情', {
                id: id,
            });
        },
        //取消订单
        do_removeorder: function (id) {
            var that = this;
            that.id = id;
            that.show = true;
        },
        beforeClose: function (action, done) {
            var that = this;
            if (action === 'confirm') {
                var option = {};
                option.OrderID = that.id;
                option.action = 'cancelorder';
                ns.post(option, function (succeed, data, err) {
                    if (succeed) {
                        if (data.status) {
                            api.toast({
                                msg: '操作成功',
                                duration: 2000,
                                location: 'bottom'
                            });
                            that.get_data();
                        }
                    } else {
                        api.toast({
                            msg: err,
                            duration: 2000,
                            location: 'bottom'
                        });
                    }
                });
                that.show = false;
                done();
            } else if (action === 'cancel') {
                done();
            }
        },
        //删除订单
        do_deleteorder: function (id) {
            var that = this;
            api.confirm({
                title: '提示',
                msg: '确定删除?',
                buttons: ['确定', '取消']
            }, function (ret, err) {
                if (ret.buttonIndex == 1) {
                    //调用删除接口
                    var opt = {
                        action: 'deleteorder', //删除的方法名
                        OrderID: id,
                    };
                    ns.post(opt, function (succeed, data, err) {
                        if (succeed) {
                            api.toast({
                                msg: '操作成功！',
                                duration: 2000,
                                location: 'bottom'
                            });
                            that.get_data();
                        } else if (err) {
                            api.toast({
                                msg: err,
                                duration: 2000,
                                location: 'bottom'
                            });
                        }
                    })
                }
            });
        },
        //确认送达
        do_sendokorder: function (id) {
            var that = this;
            ns.openWin('dl_order_send_ok', '确认送达', {
                id: id,
            });
        },
    }
});
apiready = function () {
    api.parseTapmode();
    ns = window.Foresight.Util;
    UIAlbumBrowser = api.require('UIAlbumBrowser');

    api.addEventListener({
        name: 'relod_orderlist'
    }, function (ret, err) {
        app.get_data();
    });
    
    ns.init_pull();
    app.getSearchKeywordHistory4Prd()
    app.get_data()
};
